﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using geetol.sms;

namespace geetol.sms.api
{
    public partial class editSmsContent : Form
    {
        public string _smskey, _smsvalue;
        public editSmsContent(string smskey,string smsvalue)
        {
            _smskey = smskey;
            _smsvalue = smsvalue;
            InitializeComponent();
        }

        private void editSmsContent_Load(object sender, EventArgs e)
        {
            labKey.Text = _smskey;
            txtValue.Text = _smsvalue;
            LoadSmsVar();
            btnAddVar.Click += BtnAddVar_Click;
            BtnSave.Click += BtnSave_Click;
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtValue.Text)) { MessageBox.Show("内容不能为空！");return; }
            var d = sysvar.smsContent.FirstOrDefault(o => o.smskey == _smskey);
            d.smsValue = txtValue.Text;
            sysvar.smsContentChange();
            foreach (Form f in Application.OpenForms) if (f is smsController) { ((smsController)f).LoadSmsContent();break; }
            this.Close();
        }

        private void BtnAddVar_Click(object sender, EventArgs e)
        {
            txtValue.Text += "{{"+combVar.Text+"}}";
        }

        void LoadSmsVar()
        {
            combVar.Items.Clear();
            var d = core.config.LoadSmsVar().varlist.FirstOrDefault(o=>o.smskey == _smskey);
            foreach (var t in d.smsvar) combVar.Items.Add(t.Key);
            if (combVar.Items.Count > 0) combVar.SelectedIndex = 0;
        }
    }
}
